package com.taotao.rop.service;

import com.rop.RopRequest;
import com.rop.annotation.HttpAction;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import com.rop.response.BusinessServiceErrorResponse;
import com.rop.session.SimpleSession;
import com.rop.utils.RopUtils;
import com.taotao.admin.MyRopClient;
import com.taotao.rop.database.Constant;
import com.taotao.rop.database.domain.Manager;
import com.taotao.rop.database.domain.RopSession;
import com.taotao.rop.database.mapper.ManagerMapper;
import com.taotao.rop.manager.RopSessionManager;
import com.taotao.rop.response.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@ServiceMethodBean(httpAction = HttpAction.POST, version = "1.0")
public class ManagerService {
    @Autowired
    private ManagerMapper managerMapper;

    @ServiceMethod(method = "manager.logon", needInSession = NeedInSessionType.NO)
    public Object logon(Manager request) {
        List<Manager> managers = managerMapper.getList(request);
        if (managers == null || managers.size() <= 0) {
            return new BusinessServiceErrorResponse(
                    request.getRopRequestContext().getMethod(), "WRONG_USERNAME_OR_PASSWORD",
                    request.getRopRequestContext().getLocale()
            );
        } else {
            //创建会话对象
            SimpleSession session = new SimpleSession();
            String sessionId = RopUtils.getUUID();
            session.setAttribute(RopSessionManager.KEY_SESSION_ID, sessionId);
            session.setAttribute(RopSessionManager.KEY_MANAGER_ID, managers.get(0).getId());
            session.setAttribute(RopSessionManager.KEY_ROLE_ID, managers.get(0).getRoleId());
            session.setAttribute(RopSessionManager.KEY_APP_ID, managers.get(0).getAppId());
            session.setAttribute(RopSessionManager.KEY_CLIENT_IP, request.getRopRequestContext().getIp());
            request.getRopRequestContext().addSession(sessionId, session);

            BaseResponse<String> response = new BaseResponse<String>();
            response.setSuccess(true);
            response.setData(sessionId);
            return response;
        }
    }

    @ServiceMethod(method = "manager.session")
    public Object getSession(RopRequest request) {
        SimpleSession session = (SimpleSession) request.getRopRequestContext().getSession();
        RopSession ropSession = new RopSession();
        ropSession.setSessionId(String.valueOf(session.getAttribute(RopSessionManager.KEY_SESSION_ID)));
        ropSession.setManagerId((Long) session.getAttribute(RopSessionManager.KEY_MANAGER_ID));
        ropSession.setRoleId((Long) session.getAttribute(RopSessionManager.KEY_ROLE_ID));
        ropSession.setAppId(String.valueOf(session.getAttribute(RopSessionManager.KEY_APP_ID)));

        BaseResponse<RopSession> response = new BaseResponse<RopSession>();
        response.setSuccess(true);
        response.setData(ropSession);
        return response;
    }

    @ServiceMethod(method = "manager.logout")
    public Object logout(RopRequest request) {
        //删除会话
        request.getRopRequestContext().removeSession();

        BaseResponse response = new BaseResponse();
        response.setSuccess(true);
        return response;
    }

    @ServiceMethod(method = "manager.list")
    public Object getManagerByPage(Manager request) {
        String appId = ((SimpleSession)request.getRopRequestContext().getSession()).getAttribute(RopSessionManager.KEY_APP_ID).toString();
        if (!appId.equalsIgnoreCase(MyRopClient.APP_ID)) {
            request.setAppId(appId);
        }
        List<Manager> managerList = managerMapper.getListByPage(request);

        BaseResponse<List<Manager>> response = new BaseResponse<List<Manager>>();
        response.setSuccess(true);
        response.setMsg("" + managerMapper.getCount(request));
        response.setData(managerList);
        return response;
    }

    @ServiceMethod(method = "manager.find")
    public Object findManager(Manager request) {
        List<Manager> managerList = managerMapper.getList(request);

        BaseResponse<List<Manager>> response = new BaseResponse<List<Manager>>();
        response.setSuccess(true);
        response.setMsg("" + managerMapper.getCount(request));
        response.setData(managerList);
        return response;
    }

    @ServiceMethod(method = "manager.insert")
    public Object insertManager(Manager request) {
        request.setPassword("7C4A8D09CA3762AF61E59520943DC26494F8941B");    //默认密码123456
        request.setStatus(Constant.STATUS_ENABLE);
        Date now = new Date();
        request.setCreateTime(now);
        request.setLastChangeTime(now);
        Long sessionUserId = (Long) ((SimpleSession)request.getRopRequestContext().getSession()).getAttribute(RopSessionManager.KEY_MANAGER_ID);
        request.setCreateUserId(sessionUserId);
        request.setLastChangeUserId(sessionUserId);
        Integer count = managerMapper.insertOne(request);

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }

    @ServiceMethod(method = "manager.update")
    public Object updateManager(Manager request) {
        request.setLastChangeTime(new Date());
        Long sessionUserId = (Long) ((SimpleSession)request.getRopRequestContext().getSession()).getAttribute(RopSessionManager.KEY_MANAGER_ID);
        request.setLastChangeUserId(sessionUserId);

        Integer count = managerMapper.updateOne(request);

        BaseResponse<Integer> response = new BaseResponse<Integer>();
        response.setSuccess(true);
        response.setData(count);
        return response;
    }
}
